package com.sl.au.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.ContractBillInfo;
import com.sl.au.entity.User;
import com.sl.au.repository.PaymentSearchForViewRespository;
import com.sl.au.service.PaymentSearchForViewService;
import com.sl.au.service.QueryService;


@Service("PaymentSearchForViewService")
@Transactional
public class PaymentSearchForViewServiceImpl  extends BaseServiceImpl implements PaymentSearchForViewService{
	
	@Autowired
	PaymentSearchForViewRespository paymentSearchForViewRespository;
	
	@Autowired
	QueryService queryService;
	
	public PaymentSearchForViewRespository getPaymentSearchForViewRespository() {
		return paymentSearchForViewRespository;
	}
	@Override
	public List<ContractBillInfo> getPayHistory(int start, int length, Map<String, Object> condition, User user, String orderName, String dir) {
		@SuppressWarnings("unchecked")
		HashMap<String, Object> numMap=(HashMap<String, Object>) condition.get("numMap");
		Map<String,Object>namedParamsMap=new HashMap<String,Object>();
		StringBuilder hql=queryService.buildCombinedQueryHql("ContractBillInfo",null, condition, namedParamsMap, numMap, user);
		for(String key:numMap.keySet()){
			Object value=numMap.get(key);
			if (value instanceof String){
				 if(StringUtils.hasText((String)value)){
					 hql.append("and (e."+key+" like '%"+value.toString()+"%')");
				 }
			}
			/*if (value instanceof String){
				 if(StringUtils.hasText((String)value)){
					 hql.append("and (e."+key+" like '%"+value.toString()+"%')");
				 }
			}*/
		}
		String dataFilter=condition.get("dataFilter").toString();
		hql.append("and (e.cSuperior like '%"+user.getSaleArea().getSuperior().toString()+"%')");
		hql.append(" and (e.dNotionalPoolingYearAndMonth='"+condition.get("monthYear").toString()+"')");
		System.out.println(hql.toString());
		Query query=queryService.createQueryHql(hql.toString());
		query.setFirstResult(start).setMaxResults(length);
		return queryService.executeHql(query, namedParamsMap);
//		String whereStr = " s.dNotionalPoolingYearAndMonth='" +monthYear+"' AND s.cSuperior = '"+superior+"' " ;
//		Iterator it = map.entrySet().iterator(); 
//		while (it.hasNext()) {
//			Map.Entry entry = (Map.Entry) it.next(); 
//			String key = entry.getKey().toString(); 
//			String value = entry.getValue().toString();
//			String tempKey = key.substring(1, key.length());//去掉第一位查询方式，判断字符串类型
//			if(key.startsWith("l")){//string 模糊查询
//				whereStr += " AND s."+ tempKey + " like '%"+value+"%' ";
//			}else if(key.startsWith("b")){//>=
//				if(tempKey.startsWith("n")){//数字
//					whereStr += " AND s." + tempKey + ">=" + value;
//				}else{//日期
//					whereStr += " AND s." + tempKey + ">='" + value + "' ";
//				}
//			}else if(key.startsWith("e")){//<
//				if(tempKey.startsWith("n")){//数字
//					whereStr += " AND s." + tempKey + "<=" + value;
//				}else{//日期
//					whereStr += " AND s." + tempKey + "<='" + value + "' ";
//				}
//			}
//		} 
//		if(key != null && !key.equals("")){
//			contractBillInfo=paymentSearchForViewRespository.getContractBillInfo(superior,monthYear,key,pageable);
//		}else{
//			contractBillInfo=paymentSearchForViewRespository.getContractBillInfo(superior,monthYear,pageable);
//		}
//		//contractBillInfo=paymentSearchForViewRespository.getContractBillInfo(whereStr,pageable);
//		return contractBillInfo.getContent();
	}
	
	
//	@Override
//	public long getCount(Map<String, Object> condition, User user, String orderName, String dir) {
//		String whereStr = " s.dNotionalPoolingYearAndMonth='" +monthYear+"' AND s.cSuperior = '"+superior+"' " ;
//		Iterator it = map.entrySet().iterator(); 
//		while (it.hasNext()) {
//			Map.Entry entry = (Map.Entry) it.next(); 
//			String key = entry.getKey().toString(); 
//			String value = entry.getValue().toString();
//			String tempKey = key.substring(1, key.length());//去掉第一位查询方式，判断字符串类型
//			if(key.startsWith("l")){//string 模糊查询
//				whereStr += " AND s."+ tempKey + " like '%"+value+"%' ";
//			}else if(key.startsWith("b")){//>=
//				if(tempKey.startsWith("n")){//数字
//					whereStr += " AND s." + tempKey + ">=" + value;
//				}else{//日期
//					whereStr += " AND s." + tempKey + ">='" + value + "' ";
//				}
//			}else if(key.startsWith("e")){//<
//				if(tempKey.startsWith("n")){//数字
//					whereStr += " AND s." + tempKey + "<=" + value;
//				}else{//日期
//					whereStr += " AND s." + tempKey + "<='" + value + "' ";
//				}
//			}
//		}
		//return paymentSearchForViewRespository.getCount(whereStr);
//		if(key != null && !key.equals("")){
//			return paymentSearchForViewRespository.getCount(superior,monthYear,key);
//		}else{
//			return paymentSearchForViewRespository.getCount(superior,monthYear);
//		}
//	}
}
